/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package webOffline;

import java.util.ArrayList;

/**
 *
 * @author Do Viet Trung
 */
public class Pattern {
    private String pattern;
    private PermissionPattern permission;
    private ArrayList<Extension> arrayExtension;
    public static String LIMIT_PATTERN = "(*)";
    
    public Pattern() {

    }
    
    public Pattern(String pattern, PermissionPattern permission) {
        this.pattern = pattern;
        this.permission = permission;
        arrayExtension = new ArrayList<Extension>();
    }

    public void createExtension(ArrayList<String> listExt) {
        if(this.permission == PermissionPattern.ALLOW_ALL) {//exp: /default/*
            this.arrayExtension.addAll(initArrayExtension(listExt, PermissionExtension.ALLOW));
        }
        else if(this.permission == PermissionPattern.ALLOW_ITEMS) { //default/*.jpg
            this.arrayExtension.add( new Extension(getExtension(), PermissionExtension.ALLOW));
        }
        else if(this.permission == PermissionPattern.DENY_ALL) {
            this.arrayExtension.addAll(initArrayExtension(listExt, PermissionExtension.DENY));
        }
        else if(this.permission == PermissionPattern.DENY_ITEMS) {
           this.arrayExtension.add( new Extension(getExtension(), PermissionExtension.DENY));
        }
    }
    
    public void createExtension(ArrayList<String> listExt,
                                int []listChoose,
                                PermissionExtension chooseExt,
                                PermissionExtension notChooseExt){
        ArrayList<Extension> res = initArrayExtension(listExt, notChooseExt);
        for(int i = 0; i < listChoose.length; i++){
            res.get(listChoose[i]).setPermission(chooseExt);
        }
        this.arrayExtension.addAll(res);
    }

    public ArrayList<String> getListExtensions() {
        ArrayList<String> res = new ArrayList<String>();
        for(int i = 0; i < arrayExtension.size(); i++) {
            res.add(arrayExtension.get(i).getExtension());
        }

        return res;
    }

    private ArrayList<Extension> initArrayExtension(ArrayList<String> listExt, 
                                                    PermissionExtension permission) {
        ArrayList<Extension> res = new ArrayList<Extension>();
        for(int i = 0; i < listExt.size(); i++) {
            res.add(new Extension(listExt.get(i), permission));
        }

        return res;
    }

    public String getAllowExt(){
        String value = "";
        if(permission.equals(PermissionPattern.ALLOW_ALL) == true){
            value = "all";
        } else if(permission.equals(PermissionPattern.DENY_ALL) == true){
            value = "none";
        } else if(permission.equals(PermissionPattern.DENY_ITEMS) == true){
            value = "remain";
        } else {
            for(int i = 0; i < arrayExtension.size(); i++){
                if(arrayExtension.get(i).getPermission().equals(PermissionExtension.ALLOW) == true){
                    value += i + ",";
                }
            }
            if (value.equals("") == false){
                value = value.substring(0, value.length() - 1);
            }
        }

        return value;
    }

    public String getDenyExt(){
        String value = "";
        if(permission.equals(PermissionPattern.ALLOW_ALL) == true){
            value = "none";
        } else if(permission.equals(PermissionPattern.DENY_ALL) == true){
            value = "all";
        } else if(permission.equals(PermissionPattern.ALLOW_ITEMS) == true){
            value = "remain";
        } else {
            for(int i = 0; i < arrayExtension.size(); i++){
                if(arrayExtension.get(i).getPermission().equals(PermissionExtension.DENY) == true){
                    value += i + ",";
                }
            }
            if (value.equals("") == false){
                value = value.substring(0, value.length() - 1);
            }
        }

        return value;
    }

    private String getExtension() {
        int i = this.pattern.lastIndexOf("*.");
        if(i >= 0) {
            return this.pattern.substring(i + 2);
        }

        return null;
    }

    public void copyPattern(Pattern pattern) {
        this.arrayExtension= pattern.arrayExtension;
        this.pattern = pattern.pattern;
        this.permission = pattern.permission;
    }

    public ArrayList<Extension> getArrayExtension() {
        return arrayExtension;
    }

    public void setArrayExtension(ArrayList<Extension> arrayExtension) {
        this.arrayExtension = arrayExtension;
    }

    public String getPattern() {
        return pattern;
    }

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    public PermissionPattern getPermission() {
        return permission;
    }

    public void setPermission(PermissionPattern permission) {
        this.permission = permission;
    }

    public static PatternType indentifyTpe(String pattern) {
        return PatternType.ALL;
    }
}

